<template>
  <div class='xtx-goods-page' v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">{{goods.categories[1].name}}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">{{goods.categories[0].name}}</XtxBreadItem>
        <XtxBreadItem >{{goods.name}}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 商品信息左边 -->
        <div class="goods-info-left">
          <!-- 商品图片组件 -->
          <GoodsImage :images='goods.mainPictures'/>
          <!-- 商品销售人气组件 -->
          <GoodsSales />
        </div>
        <!-- 商品信息右边 -->
        <div class="goods-info-right">
          <!-- 商品名称信息组件 -->
          <GoodsNames :goods='goods' />
          <!-- 商品sku 测试skuId='1369155873162661889'-->
         <GoodsSku :key="goods.id" :goods='goods' @change="changeSku"/>
          <!-- 数量计数器 -->
          <XtxNumbox @change="changeFn" label='数量' v-model="num" :max='goods.inventory'/>
          <!-- 加入购物车按钮 -->
          <XtxButton @click="addToCartBtn" type="primary" style="margin-top:20px;">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="goods.id"/>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
          <GoodsTabs />
          </div>
          <!-- 注意事项 -->
          <div class="goods-warn">
            <GoodsWarn />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :goodsId='goods.id' :type='1'/>
          <GoodsHot :goodsId='goods.id' :type='2'/>
          <GoodsHot :goodsId='goods.id' :type='3'/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { useStore } from 'vuex'
import Message from '@/components/library/Message'
import { useRoute } from 'vue-router'
import { findGoods } from '@/api/product'
import GoodsRelevant from './components/goods-relevant'
import { provide, ref, watch } from 'vue'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsNames from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsHot from './components/goods-hot.vue'
import GoodsWarn from './components/goods-warn.vue'
export default {
  name: 'XtxGoodsPage',
  components: { GoodsRelevant, GoodsImage, GoodsSales, GoodsNames, GoodsSku, GoodsTabs, GoodsHot, GoodsWarn },
  setup () {
    const route = useRoute()
    const goods = ref(null)
    provide('goods', goods)
    watch(() => route.params.id, async (id) => {
      if (route.path !== `/product/${id}`) return
      const { result } = await findGoods(id)
      goods.value = result
      console.log(result)
    }, { immediate: true })

    const num = ref(1)

    const changeSku = (sku) => {
      if (sku.skuId) {
        // console.log(sku)
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory
        // 选中的规格，加入购物车的时候要用到
        goods.value.attrsText = sku.specsText
        // 存储当前 skuId，加入购物车的时候要用到
        goods.value.currSkuId = sku.skuId
      } else {
        // 没有选择完整规格就重置为空
        goods.value.currSkuId = ''
      }
    }
    const changeFn = (val) => {
      goods.value.count = val
    }
    const store = useStore()
    const addToCartBtn = () => {
      if (!goods.value.currSkuId) return Message({ type: 'warn', text: '请选择完整规格' })
      if (num.value > goods.value.inventory) return Message({ type: 'warn', text: '商品库存不足' })
      // 约定本地存储的信息内容 - 字段名可参考购物车列表接口的字段名
      const cartItem = {
        id: goods.value.id, // SPUID
        name: goods.value.name, // 商品名称
        picture: goods.value.mainPictures[0], // 图片
        price: goods.value.oldPrice, // 加入时价格
        skuId: goods.value.currSkuId, // SKUID
        attrsText: goods.value.attrsText, // 属性文字，例如“颜色:瓷白色 尺寸：8寸
        nowPrice: goods.value.price, // 当前的价格
        stock: goods.value.inventory, // 库存
        count: num.value, // 数量
        selected: true, // 是否选中
        isCollect: false, // 是否收藏
        isEffective: true, // 是否为有效商品
        discount: 1// 折扣信息
      }
      console.log(cartItem)
      store.dispatch('cart/insertCart', cartItem).then(() => {
        Message({ type: 'success', text: '加入购物车成功' })
      })
    }
    return { goods, num, changeSku, addToCartBtn, changeFn }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .goods-info-left {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .goods-info-right {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
// .goods-tabs {
//   min-height: 600px;
//   background: #fff;
// }
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
